var Role=function () {

    bootbox.setDefaults({
        'locale':'zh_CN'
    });

    $.fn.serializeObject = function () {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function () {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };

    return{
        showAddModal:function(){
            $('#addRole').modal({
                show:true
            });
            $('#div_pwd').show();
            $('#addRole h4').text("添加角色");
        },
        getRole:function(id){
            $.ajax({
                url: "/api/role/getRole",
                data:{id:id},
                type:'POST',
                dataType: "json",
                success: function (data) {
                    if(data.code =='401'){
                        bootbox.alert(data.message);
                    }else if(data.code =='200'){
                        Form.loadData(data.data);
                        $('#addRole').modal({
                            show:true
                        });
                        $('#addRole h4').text("编辑角色");
                    }
                }
            });
        },
        saveRole:function () {
            var data = $('#roleForm').serializeObject();
            $.ajax({
                url: "/api/role/save",
                data:JSON.stringify(data),
                type:'POST',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    if(data.code =='401'){
                        bootbox.alert(data.message);
                    }else{
                        if(data.data){
                            $('#addRole').modal("hide");
                            $('#roleTable').dataTable().fnDraw();
                            Role.formReset();
                        }else{
                            bootbox.alert(data.message)
                        }
                    }
                }
            });
        },
        deleteRole:function (id) {
            bootbox.confirm('确定要删除该角色吗?',function(btn){
                if(btn){
                    $.ajax({
                        url: "/api/role/delete",
                        data:{id:id},
                        type:'POST',
                        dataType: "json",
                        success: function (data) {
                            if(data.code =='401'){
                                bootbox.alert(data.message);
                            }else if(data.code =='200'){
                                $('#roleTable').dataTable().fnDraw();
                            }
                        }
                    });
                }
            });
        },
        formReset:function(){
            $('#roleForm')[0].reset();
            $('#id').val('');
            $(':input','#roleForm').removeAttr('checked');
        }

    }
}();